package cn.wit.tech.service.impl;


import cn.wit.tech.mapper.CurriculumMapper;
import cn.wit.tech.mapper.ProjectExerciseMapper;
import cn.wit.tech.mapper.TechPointMapper;
import cn.wit.utils.pojo.Curriculum;
import cn.wit.utils.pojo.ProjectExercise;
import cn.wit.utils.pojo.TechPoint;
import cn.wit.tech.service.TechPointService;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class TechPointServiceImpl implements TechPointService {
    @Autowired
    private TechPointMapper techPointMapper;
    @Autowired
    private CurriculumMapper curriculumMapper;
    @Autowired
    private ProjectExerciseMapper projectExerciseMapper;


    @Override
    public Result insert(TechPoint techpoint) {
        int i = techPointMapper.insert(techpoint);
        if (i > 0) {
            return Result.ok();
        }
        return Result.error("新增失败");
    }

    @Override
    public Result deleteById(int id) {
        QueryWrapper<Curriculum> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("techpoint_ID", id);
        List<Curriculum> curriculum = curriculumMapper.selectList(wrapper1);
        if (CollectionUtils.isEmpty(curriculum)) {
            int i = techPointMapper.deleteById(id);
            if (i > 0) {
                return Result.ok();
            }
            return Result.error("删除失败");
        }
        QueryWrapper<ProjectExercise> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("techpoint_ID", id);
        List<ProjectExercise> projectExercise = projectExerciseMapper.selectList(wrapper2);
        if (CollectionUtils.isEmpty(projectExercise)) {
            int i = techPointMapper.deleteById(id);
            if (i > 0) {
                return Result.ok();
            }
            return Result.error("删除失败");
        }
        return Result.error("删除失败");
    }

    @Override
    public Result updateById(TechPoint techpoint) {
        int i = techPointMapper.updateById(techpoint);
        if (i > 0) {
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @Override
    public Result selectAllTechPoint() {
        List<TechPoint> techPoints = techPointMapper.selectList(null);
        if (!CollectionUtils.isEmpty(techPoints)) {
            return Result.ok(techPoints);
        }
        return Result.ok(new ArrayList<>());
    }

    @Override
    public Result selectAllPage(IPage<TechPoint> page) {
        IPage<TechPoint> techPointIPage = techPointMapper.selectPage(page, null);
        return Result.ok(techPointIPage);
    }
}
